package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.itheima.common.PageBean;
import com.itheima.common.Result;
import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController //等价于@Controller+@ResponseBody
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 带条件分页查询
     * @param currentPage 当前页码，普通参数
     * @param pageSize 每页条数，普通参数
     * @param brand 搜索条件，请求体+json
     * @return
     */
    @PostMapping("/{currentPage}/{pageSize}")
    public Result selectByPage(@PathVariable Integer currentPage,
                               @PathVariable Integer pageSize,
                               @RequestBody Brand brand){
        //调用service带条件分页查询，并返回分页结果，由springmvc自动使用jackson转换成json响应。
        Page page = brandService.selectByPage(currentPage, pageSize, brand);
        //分组分页结果
        PageBean pageBean=new PageBean(page.getTotal(),page.getRecords());
        return new Result(2001,"查询成功",pageBean);
    }

    /**
     * 新增品牌
     * @param brand
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Brand brand){
        //调用service新增
        brandService.save(brand);
        return new Result(2002,"新增成功",null);
    }

    /**
     * 批量删除品牌
     * @param ids
     * @return
     */
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody Integer[] ids){
        //调用service批量删除
        brandService.removeBatchByIds(Arrays.asList(ids));
        return new Result(2003,"删除成功",null);
    }

    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Brand brand){
        //调用service修改
        brandService.updateById(brand);
        return new Result(2004,"修改成功",null);
    }

}
